"""
知识库文件相关数据模型
"""
from datetime import datetime
from typing import List, Optional
from enum import Enum
from pydantic import BaseModel, Field  # pyright: ignore[reportMissingImports]
from bson import ObjectId  # pyright: ignore[reportMissingImports]
from app.models.base import PyObjectId


class FileType(str, Enum):
    """文件类型"""
    DOCUMENT = "document"  # 文档
    IMAGE = "image"        # 图片
    VIDEO = "video"        # 视频
    AUDIO = "audio"        # 音频
    OTHER = "other"        # 其他


class FileFormat(str, Enum):
    """文件格式"""
    # 文档格式
    PDF = "pdf"
    DOC = "doc"
    DOCX = "docx"
    XLS = "xls"
    XLSX = "xlsx"
    PPT = "ppt"
    PPTX = "pptx"
    TXT = "txt"
    MD = "md"
    HTML = "html"
    # 图片格式
    JPG = "jpg"
    JPEG = "jpeg"
    PNG = "png"
    GIF = "gif"
    BMP = "bmp"
    WEBP = "webp"
    SVG = "svg"
    # 视频格式
    MP4 = "mp4"
    AVI = "avi"
    MOV = "mov"
    WMV = "wmv"
    # 音频格式
    MP3 = "mp3"
    WAV = "wav"
    OGG = "ogg"
    # 其他
    ZIP = "zip"
    RAR = "rar"
    OTHER = "other"


class FilePermission(str, Enum):
    """文件权限"""
    PUBLIC = "public"   # 公开
    TEAM = "team"       # 团队
    PRIVATE = "private" # 私有




class KnowledgeFile(BaseModel):
    """知识库文件模型"""
    id: Optional[PyObjectId] = Field(default=None, alias="_id")
    title: str = Field(..., description="文件标题")
    filename: str = Field(..., description="原始文件名")
    file_path: str = Field(..., description="MinIO存储路径")
    file_size: int = Field(..., description="文件大小（字节）")
    mime_type: str = Field(..., description="MIME类型")
    file_type: FileType = Field(..., description="文件类型")
    format: FileFormat = Field(..., description="文件格式")
    folder_id: Optional[PyObjectId] = Field(default=None, description="所属文件夹ID")
    tags: List[str] = Field(default_factory=list, description="标签列表")
    description: Optional[str] = Field(default=None, description="文件描述")
    project_id: Optional[PyObjectId] = Field(default=None, description="关联项目ID（可选）")
    author_id: PyObjectId = Field(..., description="创建者ID")
    organization_id: Optional[PyObjectId] = Field(default=None, description="组织ID")
    is_public: bool = Field(default=False, description="是否公开")
    permission: FilePermission = Field(default=FilePermission.PRIVATE, description="权限")
    download_count: int = Field(default=0, description="下载次数")
    view_count: int = Field(default=0, description="查看次数")
    favorite_count: int = Field(default=0, description="收藏次数")
    status: str = Field(default="", description="状态（从状态管理系统动态获取，创建时使用默认状态）")
    created_at: datetime = Field(default_factory=datetime.utcnow, description="创建时间")
    updated_at: datetime = Field(default_factory=datetime.utcnow, description="更新时间")
    deleted_at: Optional[datetime] = Field(default=None, description="删除时间（软删除）")

    class Config:
        populate_by_name = True
        arbitrary_types_allowed = True
        json_encoders = {
            ObjectId: str,
            datetime: lambda v: v.isoformat()
        }

